package com.luojun.service;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.luojun.dao.CarDao;
import com.luojun.entity.Car;
import com.luojun.util.HibernateUtil;

public class CarService {
	Session session =null;
	Transaction tran = null;
	CarDao cardao = new CarDao();
	/**
	 * 添加的service层
	 * @param car
	 */
	public void save(Car car){
		if(car!=null){
		session =HibernateUtil.getCurrentSession();
		tran = session.beginTransaction();
		car.setQuantity(1);
		cardao.save(car);
		}else{
			System.out.println("car为null");
		}
		tran.commit();
	}
	/**
	 * 添加的service层
	 * @param car
	 */
	public void buy(Car car){
		if(car!=null){
		session =HibernateUtil.getCurrentSession();
		tran = session.beginTransaction();
		car.setQuantity(0);
		cardao.save(car);
		}else{
			System.out.println("car为null");
		}
		tran.commit();
	}
	/**
	 * 删除的service层
	 * @param ID
	 */
	public void del(int userid ,int commodityid){
		session =HibernateUtil.getCurrentSession();
		tran = session.beginTransaction();
		cardao.del(userid,commodityid);
		tran.commit();
	}
	/**
	 * 给我也整一个
	 * @param commodityId
	 */
	public void mai(Car car){
		session =HibernateUtil.getCurrentSession();
		tran = session.beginTransaction();
		cardao.update(car);
		tran.commit();
	}
	/**
	 * 买就完事了
	 * @param userId
	 */
	public List<Car> maimaimai(int userId){
		session =HibernateUtil.getCurrentSession();
		tran = session.beginTransaction();
		List<Car> list  = cardao.find(userId);
		
		tran.commit();
		for (Car car : list) {
			car.setQuantity(0);
			mai(car);
		}
		return list;
	}
	/**
	 * 查找
	 * @param userId
	 * @return
	 */
	public List<Car> find(int userId){
		session =HibernateUtil.getCurrentSession();
		tran = session.beginTransaction();
		List<Car> list = cardao.find(userId);
		tran.commit();
		return list;
		
	}
	
	/**
	 * 查找
	 * @param userId
	 * @return
	 */
	public Car findbycommodityID(int commodityID){
		session =HibernateUtil.getCurrentSession();
		tran = session.beginTransaction();
		List<Car> list = cardao.findbycommodityID(commodityID);
		Car car = list.get(0);
		tran.commit();
		return car;
		
	}
	/**
	 * 查找
	 * @param userId
	 * @return
	 */
	public List<Car> findsell(int userId){
		session =HibernateUtil.getCurrentSession();
		tran = session.beginTransaction();
		List<Car> list = cardao.findsell(userId);
		tran.commit();
		return list;
		
	}
	
}
